<!--
 * @Author: wankong
 * @Desc: 
 * @Date: 2021-10-10 20:13:19
 * @LastEditTime: 2021-11-03 19:27:47
-->
<template>
  <div class="Comments">
    <el-table :data="commentList">
      <el-table-column type="index"></el-table-column>
      <el-table-column label="内容">
        <template slot-scope="scope">
          <span>{{ scope.row.content }} </span>
          <br />
          <span class="sfont">{{ scope.row.ctime | timeFormatter }} </span>
        </template>
      </el-table-column>
      <el-table-column label="评论者" width="200px">
        <template slot-scope="scope">
          <p>{{ scope.row.nickname }}</p>
          <p class="sfont">QQ: {{ scope.row.userqq }}</p>
        </template>
      </el-table-column>
      <el-table-column label="是否可见" width="100px">
        <template slot-scope="scope">
          <el-switch
            @change="statusChange(scope.row)"
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="所属文章" width="100px">
        <template slot-scope="scope">
          <el-link :href="goView(scope.row)" target="_blank">
            <i class="el-icon-view"></i>
          </el-link> </template
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="reBackUser(scope.row)" type="warning">
            回复
          </el-button>
          <el-button @click="DelComments(scope.row)" type="danger">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="query.page"
      :page-size="query.page"
      layout="sizes,prev, pager, next"
      @size-change="sizeChange"
      @current-change="pageChange"
    >
    </el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="回复评论"
      :visible.sync="dialogVisible"
      width="40%"
      @close="dialogClose"
    >
      <el-form :model="comment" ref="recommentRefs" label-width="100px">
        <el-form-item label="回复给：">
          <p
            style="
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
            "
          >
            {{ reComment.content }}
          </p>
        </el-form-item>
        <el-form-item label="QQ：">
          <el-input v-model="comment.qq"></el-input>
        </el-form-item>
        <el-form-item label="回复内容：">
          <el-input type="textarea" v-model="comment.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitComment">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0,
      query: {
        page: 1,
        limit: 10,
      },
      dialogVisible: false,
      commentList: null,
      reComment: {},
      comment: {
        bid: 0,
        fid: 0,
        qq: "1549072621",
        content: "",
      },
    };
  },
  computed: {
    goView() {
      return function (row) {
        return this.$router.resolve(`/article/${row.bid}`).href;
      };
    },
  },
  methods: {
    async getCommentsList() {
      const res = await this.$http.get(`${this.adminPre}/comments`, {
        params: this.query,
      });
      this.total = res.data.total;
      this.commentList = res.data.lists;
    },
    async statusChange(row) {
      const comment = Object.assign({}, row);
      const res = await this.$http.post(`${this.adminPre}/comments`, comment);
      if (res.code == 200) {
        return this.$message.success("修改成功！");
      } else {
        return this.$message.error("修改失败！");
      }
    },
    sizeChange(nowsize) {
      console.log(nowsize);
      this.query.limit = nowsize;
      this.getCommentsList();
    },
    pageChange(nowpage) {
      this.query.page = nowpage;
      this.getCommentsList();
    },
    dialogClose() {
      this.reComment = {};
      this.comment = {
        bid: 0,
        fid: 0,
        qq: "1549072621",
        content: "",
      };
      this.dialogVisible = false;
    },
    /**
     * 回复当前评论
     */
    reBackUser(row) {
      this.reComment = row;
      this.comment.content = `回复@${row.nickname}:`;
      this.dialogVisible = true;
    },
    /**
     * 删除当前评论
     */
    DelComments(row) {
      this.$confirm("此操作将永久删除该评论, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(async () => {
          const res = await this.$http.delete(
            `${this.adminPre}/comments/${row.id}`
          );
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getCommentsList();
          } else {
            this.$message({
              type: "info",
              message: "删除失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /**
     * 提交回复评论
     */
    async submitComment() {
      // 拼接提交内容
      // 发起 提交 请求
      const res = await this.$http.post(
        `${this.adminPre}/comments`,
        this.comment
      );
      if (res.code == 200) {
        this.$message.success("回复成功！");
        this.getCommentsList();
        this.dialogVisible = false;
      } else {
        this.$message.error("回复失败");
      }
      // 判断执行结果
    },
  },
  created() {
    this.getCommentsList();
  },
};
</script>

<style lang="less" scoped>
.Comments {
  .sfont {
    font-size: small;
    color: #778899;
  }
}
</style>